/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.order.service;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.order.dao.CustomerDAO;
import com.javaeye.order.dao.PurchaseOrderDAO;
import com.javaeye.order.dto.Customer;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.web.PageInfo;
import com.javaeye.common.web.ReportData;

public class CustomerService implements ICustomerService {
	
	private static Log log = LogFactory.getLog(CustomerService.class);
	
	private CustomerDAO dao;
	
	private PurchaseOrderDAO purchaseOrderDao;
	
	public void setDao(CustomerDAO dao) {
		this.dao = dao;
	}
	
	public void setPurchaseOrderDao(PurchaseOrderDAO purchaseOrderDao) {
		this.purchaseOrderDao = purchaseOrderDao;
	}

	public List<Customer> getAutoCompleteCustomers(String name, int size) {
		List<Customer> result = dao.getAutoCompleteCustomers(name, size);
		if (result == null || result.size() == 0) {
			log.warn("在数据库中未找到产品数据");
		}
		return result;
	}

	@Override
	public Customer getCustomer(String id) {
		int intId = Integer.parseInt(id);
		Customer customer = dao.getCustomer(intId);
		if (customer == null) {
			log.warn("在数据库中未找到指定的产品  id：" + id);
		}
		return customer;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> getCustomerList(Customer condition, PageInfo pageInfo) {
		List results = dao.getPageData(condition, pageInfo);
		if (results == null || results.size() == 0) {
			log.warn("在数据库中未找到产品数据");
		}
		return results;
	}

	@Override
	public boolean removeCustomer(String id) {
		int intId = Integer.parseInt(id);
		//判断其他表中是否使用了客户
		long count = purchaseOrderDao.getCountByCustomerId(intId);
		if (count > 0) {
			return false;
		}
		dao.removeCustomer(intId);
		return true;
	}

	@Override
	public Customer saveCustomer(Customer customer) {
		int id = customer.getId();
		if (id == 0) {
			customer.setCreateDate(DateUtils.formatString(new Date(), "yyyyMMdd"));
		}
		dao.saveCustomer(customer);
		return customer;
	}
	
	public List<ReportData> getNewCustomerAtMonth(int month) {
		List<ReportData> reportDatas = new ArrayList<ReportData>();
		ReportData data;
		String monthString;
		for (int i = 0; i < month; i++) {
			monthString = DateUtils.getMonthFromNow(i - month + 1, "yyyyMM");
			long count = dao.getNewCustomerAtMonth(monthString);
			data = new ReportData();
			data.setColumn(DateUtils.getMonthFromNow(i - month + 1, "yyyy-MM"));
			data.setData(count);
			reportDatas.add(data);
		}
		return reportDatas;
	}

}
